package com.multiplayer.dao.impl;

import java.util.ArrayList;
import java.util.List;

import com.multiplayer.dao.ISeatDao;
import com.multiplayer.entity.Seat;

import com.multiplayer.tools.DBTools;
import com.multiplayer.tools.PageData;

/**
 * 
 * @author 温在鑫
 *@version   [版本号, 2020-05-08]
 *座位操作实现类
 */
public class SeatDaoImpl implements ISeatDao{

	@SuppressWarnings("unchecked")
	@Override
	/**
	 * 添加座位
	 */
	public boolean addSeat(int seatsNum, String studyroomId) {
		// TODO Auto-generated method stub
		//获得当前自习室的最大座位编号maxSeatId
		String sql1="select  max(eseatno) as eseatno from seat where rno=?";
		List<Seat> list=new ArrayList<Seat>();
		list=(List<Seat>) DBTools.exQuery(sql1, Seat.class,studyroomId);	
		int maxSeatId=0;
		if(list.get(0).getEseatno()==null)
		{
			String seatId=studyroomId+"001";
			maxSeatId=Integer.parseInt(seatId);
		}
		else
		{
			maxSeatId=Integer.parseInt(list.get(0).getEseatno());
		}
		
		int n=0;
		int m=0;
		//循环执行插入seat表语句
		String sql = "insert into seat(eseatno,rno,estatus) value(?,?,?)";
		for (int i = maxSeatId + 1; i <= maxSeatId + seatsNum; i++)
		{
			String seatId=i+"";
			n=DBTools.exUpdate(sql,seatId,studyroomId,"未预约");
		}
		//修改studyroom的总座位数和可预约座位数
		String sql2="update studyroom set rseatnum=rseatnum+?,rfreeseat=rfreeseat+? where rno=?";
		m=DBTools.exUpdate(sql2,seatsNum,seatsNum,studyroomId);
		if(m>0&&n>0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	/**
	 * 批量删除座位
	 */
	@Override
	public boolean deleteSeatByNum(int seatsNum, String studyroomId) {
		// TODO Auto-generated method stub
		//获得当前自习室的最大座位编号maxSeatId
		String sql1="select  max(eseatno) as eseatno from seat where rno=?";
		List<Seat> list=new ArrayList<Seat>();
		list=(List<Seat>) DBTools.exQuery(sql1, Seat.class,studyroomId);
		int maxSeatId=Integer.parseInt(list.get(0).getEseatno());	
		int n=0;
		int m=0;
		//循环执行插入seat表语句
		String sql="delete from seat where eseatno=?";
		for (int i = maxSeatId; i >maxSeatId - seatsNum; i--)
		{
			String seatId=i+"";
			n=DBTools.exUpdate(sql,seatId);
		}
		//修改studyroom的总座位数和可预约座位数
		String sql2="update studyroom set rseatnum=rseatnum-?,rfreeseat=rfreeseat-? where rno=?";
		m=DBTools.exUpdate(sql2,seatsNum,seatsNum,studyroomId);
		if(m>0&&n>0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	/**
	 * 通过座位编号删除座位
	 */
	@Override
	public boolean deleteSeatById(String seatId) {
		// TODO Auto-generated method stub
		//查询该座位所属的自习室编号
		String sql3="select * from seat where eseatno=?";
		List<Seat> seatList=new ArrayList<Seat>();
		seatList=(List<Seat>) DBTools.exQuery(sql3, Seat.class,seatId);
		String studyroomId=seatList.get(0).getRno();
		//从seat表中删除该座位
		String sql="delete from seat where eseatno=?";
		int n=DBTools.exUpdate(sql,seatId);
		int m=0;
		//修改studyroom的总座位数和可预约座位数
		String sql2="update studyroom set rseatnum=rseatnum-?,rfreeseat=rfreeseat-? where rno=?";
		m=DBTools.exUpdate(sql2,1,1,studyroomId);
		if(m>0&&n>0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	/**
	 * 修改座位信息
	 */
	@Override
	public boolean editSeat(Seat seat) {
		// TODO Auto-generated method stub
		String sql="update seat set rno=?,estatus=? where eseatno=?";
		int n=DBTools.exUpdate(sql,seat.getRno(),seat.getEstatus(),seat.getEseatno());
		return n>0;
	}

	/**
	 * 模糊查询座位信息
	 */
	@Override
	public List<Seat> querySeatByKeywords(String keywords ) {
		// TODO Auto-generated method stub
		String sql="select * from seat where eseatno like ? or rno like ? or estatus like ?";
		List<Seat> list= new ArrayList<Seat>();
		list=(List<Seat>) DBTools.exQuery(sql, Seat.class,"%"+keywords+"%","%"+keywords+"%","%"+keywords+"%" );
		return list;
	}
	/**
	 * 通过模糊查询分页输出座位信息
	 */
	@Override
	public PageData<Seat> getSeatByPage(int page, int pageSize, String keywords) {
		// TODO Auto-generated method stub
		PageData<Seat> pd = DBTools.exQueryByPage("select * from seat where eseatno like ? or rno like ? or estatus like ?"
				, Seat.class

				,page, pageSize,keywords,keywords,"%"+keywords+"%");

		System.out.println("pd:"+pd);



		return pd;
	}
	@Override
	public List<Seat> querySeatByThree(String eseatno, String restime, String resetime) {
		// TODO Auto-generated method stub
		String sql="select * from seat where eseatno like ? and rno like ? and estatus like ?";
		List<Seat> list= new ArrayList<Seat>();
		list=(List<Seat>) DBTools.exQuery(sql, Seat.class,"%"+eseatno+"%","%"+restime+"%","%"+resetime+"%" );
		return list;
	}

}
